<?php

/**
 * 静态分页类
 * 
 * @package    01CMS
 * @subpackage system
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class StaticPages
{
    public $baseUrl = '';
    public $firstFileName = '';
    public $fileId = '';
    public $fileExt = '.html';
    public $totalPages = '';
    public $pageSize = '';
    public $numLinks = 5;
    public $curPage = 1;
    public $firstLink = '&lsaquo; 首页';
    public $nextLink = '下一页';
    public $prevLink = '上一页';
    public $lastLink = '尾页 &rsaquo;';
    public $fullTagOpen = '';
    public $fullTagClose = '';
    public $firstTagOpen = '';
    public $firstTagClose = '&nbsp;';
    public $lastTagOpen = '&nbsp;';
    public $lastTagClose = '';
    public $curTagOpen = '&nbsp;<strong>';
    public $curTagClose = '</strong>';
    public $nextTagOpen = '&nbsp;';
    public $nextTagClose = '&nbsp;';
    public $prevTagOpen = '&nbsp;';
    public $prevTagClose = '';
    public $numTagOpen = '&nbsp;';
    public $numTagClose = '';
    public $Base;

    function __construct ()
    {
        $this->Base = & Base::i();
    }

    function initialize ($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = $val;
                }
            }
        }
    }

    function createLinks ($params)
    {
        if (count($params) > 0)
        {
            $this->initialize($params);
        }
        
        if (! ($this->totalPages > 0))
        {
            $this->Base->pagesCode = '<span>没有记录</span>';
            return '';
        }
        
        if ($this->totalPages == 1)
        {
            $this->Base->pagesCode = '<span>共1页</span>';
            return '';
        }
        
        $this->curPage = (int) $this->curPage;
        $this->numLinks = (int) $this->numLinks;
        
        if (! is_numeric($this->curPage))
        {
            $this->curPage = 1;
        }
        
        if ($this->curPage > $this->totalPages)
        {
            $this->curPage = $this->totalPages;
        }
        
        $start = (($this->curPage - $this->numLinks) > 0) ? $this->curPage - ($this->numLinks - 1) : 1;
        
        $end = (($this->curPage + $this->numLinks) < $this->totalPages) ? $this->curPage + $this->numLinks : $this->totalPages;
        
        $output = '';
        
        if ($this->curPage > $this->numLinks)
        {
            $output .= $this->firstTagOpen . '<a href="' . $this->baseUrl . '/' . $this->firstFileName . '">' . $this->firstLink . '</a>' . $this->firstTagClose;
        }
        
        if ($this->curPage > 2)
        {
            $output .= $this->prevTagOpen . '<a href="' . $this->baseUrl . '/' . $this->fileId . ($this->curPage - 1) . $this->fileExt . '">' . $this->prevLink . '</a>' . $this->prevTagClose;
        }
        
        for ($loop = $start; $loop <= $end; $loop ++)
        {
            if ($loop > 0)
            {
                if ($this->curPage == $loop)
                {
                    $output .= $this->curTagOpen . $loop . $this->curTagClose; // Current page
                }
                else
                {
                    $n = ($loop == 1) ? "/{$this->firstFileName}" : "/" . $this->fileId . $loop . $this->fileExt;
                    $output .= $this->numTagOpen . '<a href="' . $this->baseUrl . $n . '">' . $loop . '</a>' . $this->numTagClose;
                }
            }
        }
        
        if ($this->curPage < $this->totalPages)
        {
            $output .= $this->nextTagOpen . '<a href="' . $this->baseUrl . '/' . $this->fileId . ($this->curPage + 1) . $this->fileExt . '">' . $this->nextLink . '</a>' . $this->nextTagClose;
        }
        
        if (($this->curPage + $this->numLinks) < $this->totalPages)
        {
            $output .= $this->lastTagOpen . '<a href="' . $this->baseUrl . '/' . $this->fileId . $this->totalPages . $this->fileExt . '">' . $this->lastLink . '</a>' . $this->lastTagClose;
        }
        
        $output = preg_replace("#([^:])//+#", "\\1/", $output);
        
        $output = $this->fullTagOpen . $output . $this->fullTagClose;
        
        if ($this->pageSize > 1)
        {
            $output .= " <span>({$this->pageSize}条/页  共{$this->totalPages}页)</span> ";
        }
        
        $this->Base->pagesCode = $output;
        
        return '';
    }
}

// End